#!/usr/bin/env python
# -*- coding: utf8 -*-

from qtpy import QtWidgets, QtGui, QtCore
import random
def get_text_image(image_size=[200, 200],
                   text='sheen',
                   text_size=200,
                   background_color=None,
                   text_color=None,
                   bold=True,
                   mask=None, parent=None):
    
    result_size = QtCore.QSize(image_size[0], image_size[1])
    if background_color:
        result_background_color = QtGui.QColor(background_color[0], background_color[1], background_color[2], 255)
    else:
        result_background_color = QtGui.QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), 255)
    if text_color:
        result_text_color = QtGui.QColor(text_color[0], text_color[1], text_color[2], 255)
    else:
        result_text_color = QtGui.QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255), 255)
    result_image = QtGui.QImage(result_size, QtGui.QImage.Format_ARGB32)
    result_image.fill(result_background_color)

    # 为这个QImage构造一个QPainter
    painter = QtGui.QPainter(result_image)
    painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
    painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing, True)
    painter.setRenderHint(QtGui.QPainter.SmoothPixmapTransform, True)

    # 设置画刷的组合模式CompositionMode_SourceOut这个模式为目标图像在上
    # 改变组合模式和上面的填充方式可以画出透明的图片。
    painter.setCompositionMode(QtGui.QPainter.CompositionMode_Source)#CompositionMode_DestinationOver)

    # 改变画笔和字体
    pen = painter.pen()
    pen.setColor(result_text_color)
    font = painter.font()
    # 加粗
    font.setBold(bold)
    # 改变字体大小
    if text_size > image_size[1] or text_size <= 0:
        text_size = image_size[1]*0.6
    font.setPixelSize(text_size)

    painter.setPen(pen)
    painter.setFont(font)

    # 将text写在Image的中心
    painter.drawText(result_image.rect(), QtCore.Qt.AlignCenter, text)
    painter.end()

    pixmap = QtGui.QPixmap.fromImage(result_image)
    if mask:
        result_pixmap = add_image_mask(pixmap, mask)
    else:
        result_pixmap = pixmap
    # # 这个为图片的压缩度。0/100
    # n = 100
    # # 将画好的图片保存起来,可以将png改为其他格式
    # result_image.save("text.png","PNG",n)

    return result_pixmap

def add_image_mask(image, mask):
    size = image.size()

    result_image = QtGui.QImage(size, QtGui.QImage.Format_ARGB32_Premultiplied)
    mask_pixmap = QtGui.QPixmap(mask)

    painter = QtGui.QPainter(result_image)
    painter.setCompositionMode(QtGui.QPainter.CompositionMode_Source)
    painter.fillRect(result_image.rect(), QtCore.Qt.transparent)

    painter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOver)
    painter.drawPixmap(0, 0, mask_pixmap.scaled(size, QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation))
    painter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOut)
    painter.drawPixmap(0, 0, image.scaled(size, QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation))

    painter.setCompositionMode(QtGui.QPainter.CompositionMode_DestinationOver)
    painter.end()
    
    result_pixmap = QtGui.QPixmap.fromImage(result_image)
    return result_pixmap


    
    
def get_mask_pixmap(image, mask, size, border=0):
    source_image = QtGui.QPixmap(image)
    mask_image = QtGui.QPixmap(mask)
    result_size = QtCore.QSize(size[0], size[1])
    result_image = QtGui.QImage(result_size, QtGui.QImage.Format_ARGB32_Premultiplied)

    painter = QtGui.QPainter(result_image)
    painter.setCompositionMode(QtGui.QPainter.CompositionMode_Source)
    painter.fillRect(result_image.rect(), QtCore.Qt.transparent)
    # painter.fillRect(result_image.rect(), QtCore.Qt.red)
    
    
    if border:
        # print('?'*100)
        painter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOut)
        painter.drawPixmap(0, 0, mask_image.scaled(result_size, QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation))
        painter.fillRect(result_image.rect(), QtCore.Qt.white)
        painter.setCompositionMode(QtGui.QPainter.CompositionMode_DestinationOver)
        painter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOver)
        pix = get_mask_pixmap(image, mask, [size[0]-2*border, size[1]-2*border])
        # painter.end()
        # return pix
        painter.drawPixmap(border, border, pix)
        painter.setCompositionMode(QtGui.QPainter.CompositionMode_DestinationOver)
    else:
        _mask = mask_image.scaled(result_size, QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation)
        # print(source_image.size().x)
        painter.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOver)
        painter.fillRect(_mask.rect(), QtCore.Qt.black)
        _source = source_image.scaled(result_size, QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation)

        painter.drawPixmap((size[0]-_source.size().width())/2.0, (size[1]-_source.size().height())/2.0, _source)
        painter.setCompositionMode(QtGui.QPainter.CompositionMode_DestinationOut)
        painter.drawPixmap(0, 0, _mask)
        # painter.drawPixmap(0, 0, source_image.scaled(result_size, QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation))
        # painter.setCompositionMode(QtGui.QPainter.CompositionMode_DestinationOver)
        
        
        
        
    painter.end()
    result_pixmap = QtGui.QPixmap.fromImage(result_image)
    return result_pixmap

def get_size_image(color, size):
    pix = QtGui.QPixmap(size[0], size[1])
    pix.fill(color)
    return pix

def get_border_thumbnail(size, path, radius=0, pos='1111', full=False):
        pixmap = QtGui.QPixmap(path)
        pix_size = pixmap.size()
        if pix_size.width() == 0 or pix_size.height() == 0:
            return
        _label_size = QtCore.QSize(size)
        
        label_size = QtCore.QSize(size)
        if full:
            if pix_size.width() < label_size.width() or pix_size.height() < label_size.height():
                if pix_size.width() / float(pix_size.height()) > label_size.width() / float(label_size.height()):
                    # 按照高度缩放
                    _label_size.setHeight(label_size.height())
                    _label_size.setWidth(int(pix_size.width() * label_size.height() / float(pix_size.height())))
                else:
                    _label_size.setWidth(label_size.width())
                    _label_size.setHeight(int(pix_size.height() * label_size.width() / float(pix_size.width())))
            elif pix_size.width() > label_size.width() and pix_size.height() > label_size.height():
                if pix_size.width() / float(pix_size.height()) > label_size.width() / float(label_size.height()):
                    # 按照高度缩放
                    _label_size.setHeight(label_size.height())
                    _label_size.setWidth(int(pix_size.width() * label_size.height() / float(pix_size.height())))
                else:
                    _label_size.setWidth(label_size.width())
                    _label_size.setHeight(int(pix_size.height() * label_size.width() / float(pix_size.width())))

        pixmap = pixmap.scaled(_label_size, QtCore.Qt.KeepAspectRatio,
                            QtCore.Qt.SmoothTransformation)
        p_size = pixmap.size()
        _target = QtGui.QPixmap(size)  
        _target.fill(QtCore.Qt.transparent)   
        painter = QtGui.QPainter(_target)
   
        painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
        painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing, True)
        painter.setRenderHint(QtGui.QPainter.SmoothPixmapTransform, True)
        if radius:
            path = get_border_painter_path(size, radius, pos)
            painter.setClipPath(path)

        painter.drawPixmap(-(p_size.width() - size.width()) / 2, -(p_size.height() - size.height()) / 2, pixmap)
        painter.end()
        return _target

def get_border_painter_path(size, radius, pos):
    if pos =='1111':
        _path = QtGui.QPainterPath()
        _path.addRoundedRect(0, 0, size.width(), size.height(), radius, radius)
        return _path

    _path = QtGui.QPainterPath()
    _path.addRect(0, 0, size.width(), size.height())
    if pos == '0000':
        return _path

    if pos[0] == '1':
        # /** top left */
        _round = QtGui.QPainterPath()
        _round.addRoundedRect(0, 0, size.width()*2, size.height()*2, radius, radius)
        _path = _path.intersected(_round)

    if pos[1] == '1':
        # /** top right */
        _round = QtGui.QPainterPath()
        _round.addRoundedRect(-size.width(), 0, size.width()*2, size.height()*2, radius, radius)
        _path = _path.intersected(_round)
    if pos[2] == '1':
        # /** bottom right */
        _round = QtGui.QPainterPath()
        _round.addRoundedRect(-size.width(), -size.height(), size.width()*2, size.height()*2, radius, radius)
        _path = _path.intersected(_round)
    if pos[3] == '1':
        # /** bottom left */
        _round = QtGui.QPainterPath()
        _round.addRoundedRect(0, -size.height(), size.width()*2, size.height()*2, 5, 5)
        _path = _path.intersected(_round)
    return  _path